package puppy.graph;



import java.util.Enumeration;

import org.apache.commons.collections15.Transformer;

import algorithms.Constants;

import edu.uci.ics.jung.algorithms.scoring.util.VEPair;



public class DeliciousEdgeTransformer implements
		Transformer<DeliciousEdge, Double> {

	AgeAggregator agg = null;

	/*
	 * TODO
	 */
	public DeliciousEdgeTransformer(AgeAggregator agg) {
		
		
		
		this.agg=agg;

	}

	/**
	 * 
	 * Returns the probability p(tag|url)
	 * 
	 * @param edge
	 * @return
	 */
	public double calculateConditionalProbTU(DeliciousEdge edge) {

		double tu =edge.getProb();
		double url_c = edge.url.getProb();

	
		return tu / url_c;

	}

	public double calculateConditionalProbUT(DeliciousEdge edge) {

		double tu = edge.getProb();
		double url_c = edge.tag.getProb();
		
		return tu / url_c;

	} 
	
	public double calculateConditionalProb(DeliciousEdge edge) {

		if(edge.tag_to_url()){
			
			return calculateConditionalProbTU(edge);
		}
		return calculateConditionalProbUT(edge);
	} 

	public double calculateJoinProb(DeliciousEdge edge) {
		double tu = edge.getProb();
		double total = algorithms.Constants.TOTAL_EDGE_MASS;
		
		return tu / total;

	}
	
	
	
	public double getEdgeFreq(DeliciousEdge edge) {
		double tu = edge.getProb();
		
		
		return tu;

	}

	/**TODO: Verify this assumption
	 * Assuming B is the node given A --> B,
	 * 
	 * @param pair
	 * @return
	 */
	
	@Override
	public Double transform(DeliciousEdge input) {
		// TODO Auto-generated method stub

	//	System.out.println("Edge type in Transformer:" + input.type);
		// Direction false for URL -> tag ; true for tag -> URL
		if (input.url_to_tag()) {
			
					return calculateConditionalProbUT(input);

		} else {
	
			return calculateConditionalProbTU(input);
		}
	}

	
	public double calculateBackgroundProb(DeliciousNode node) {

		double total = 0.0;
		if (!node.isTag()) {

			total = Constants.TOTAL_TAG_ADULTS;

		}

		if (node.isTag()) {

			total = Constants.TOTAL_URL_ADULTS;

		}
		
		double n = node.getProb_adults() + 1;
		if (node.getName().equals("studios")) {
			// System.out.println("Prior:" + "\t"+ n + "\t" +total +"\t"+
			// agg.teens_mteens + "\t" + node.freq_type.get("teens-mteens") +
			// "\t"+ (double)n/(double)Constants.TOTAL_NODE_MASS + "\t"+
			// node.getId() );
			// printInfo(node.freq_type);
		}
		// return (double)n/(double)Constants.TOTAL_NODE_MASS;
		total = total;
		return ((double) n / (double) total);

	}

	public double calculateKL(DeliciousNode node) {

		double fore = calculateForegroundProb(node);
		double back = calculateBackgroundProb(node);

		double kl = fore * Math.log(back) - fore * Math.log(fore);

		// double kl = Math.log(fore/back);
		// if(node.getName().equals(""))
		// System.out.println(node.getName()+"\tfore:"+ fore + "\tback: "+ back
		// + "\tscore:"+ kl );
		return kl;

	}

	public double calculateForegroundProb(DeliciousNode node) {
		double total = 0.0;
		if (!node.isTag()) {

			// total= agg.getAggregate(totals_urls);
			total = Constants.TOTAL_URL_KIDS;

		}

		if (node.isTag()) {

			// total= agg.getAggregate(totals_tags);
			total = Constants.TOTAL_TAG_KIDS;

		}
		double n = node.getProb();
		
		return (double) n / (double) total;

	}

}
